#include<bits/stdc++.h>
#define int long long
#define endl "\n"

using namespace std;

signed main() {
	int t;
	cin >> t;
	while(t -- ) {
		int n, ret = 0x3f3f3f3f;
		string s;
		cin >> n >> s;
		map<char, vector<int>>mp;
		for(int i = 0; i < n; i ++ ) {
			mp[s[i]].push_back(i);
		}
		for(char i = 'a'; i <= 'z'; i ++ ) {
			vector<int>v = mp[i];
			for(int j = 1; j < v.size(); j ++ ) {
				ret = min(ret, v[j] - v[j - 1] - 1);
				ret = min(ret, (n - 1) - v[j] + v[j - 1]);
			}
			if(v.size() > 1)
			ret = min(ret, (n - 1) - v[v.size() - 1] + v[0]);
		}
		if(ret == 0x3f3f3f3f)
		ret = -1;
		cout << ret << endl;
	}
	return 0;
}
